﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Browser;
using SoftSource.Timecard.ReportDataClasses;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard
{
    public static class ReportManager
    {
        public static object GetDataObject(this Report report)
        {
            if (report == null)
                return null;

            return report.TemplateKey.GetDataObjectType();
        }

        public static object GetDataObjectType(this string templateKey)
        {
            if (string.IsNullOrEmpty(templateKey))
                return null;

            switch (templateKey)
            {
                case "StartEndDateReportTemplate":
                    return new StartEndDateReportTemplateData();
                case "StartDateReportTemplate":
                    return new DateReportTemplateData(-1);
                case "EndDateReportTemplate":
                case "DateReportTemplate":
                    return new DateReportTemplateData(0);
                case "UserStartEndDateReportTemplate":
                    return new UserStartEndDateReportTemplateData();
                case "ProjectEndDateReportTemplate":
                    return new ProjectEndDateReportTemplateData();
                case "ProjectStartEndDateReportTemplate":
                    return new ProjectStartEndDateReportTemplateData();
                default:
                    return null;
            }
        }

        public static void OpenReport(this Report report, object reportDataObject)
        {
            string uri = string.Format(@"ReportViewer.aspx?ReportName={0}", HttpUtility.UrlEncode(report.ReportName));

            if (reportDataObject != null)
            {
                var propertyBag = reportDataObject.GetPropertyBag();
                foreach (var item in propertyBag)
                {
                    string itemString = string.Empty;

                    if (item.Value is DateTime)
                        itemString = ((DateTime)item.Value).ToShortDateString();
                    else
                        itemString = item.Value.ToString();

                    uri = string.Format("{0}&{1}={2}", uri, item.Key, HttpUtility.UrlEncode(itemString));
                }
            }

            HtmlPage.Window.Navigate(new Uri(uri, UriKind.Relative), "_blank");
        }

        private static Dictionary<string, object> GetPropertyBag(this object dataObject)
        {
            Dictionary<string, object> propertyBag = new Dictionary<string, object>();

            var props = dataObject.GetType().GetProperties();

            foreach (var prop in props)
            {
                if (prop.GetCustomAttributes(false).Any(attribute => attribute is ParameterAttribute))
                {
                    propertyBag.Add(prop.Name, prop.GetValue(dataObject, null));
                }
            }

            return propertyBag;
        }
    }
}
